/******************************************************************************
 * Copyright (C) 2011 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.excel.officeexport.excel.tag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.code.common.excel.officeexport.excel.exception.ExportExcelException;


/**
 * "iterator" 标签处理类.
 * 
 * @author 李广
 * @since 1.0
 * @version 2005-1-26 李广 新建
 */
public class TageList implements ITageDeal {
    
    /** ORDER 属性 */
    private static final String ORDER = "order";
    
    /** 日志管理类对象 */
    private final Logger loger = Logger.getLogger(TageList.class);
    
    /**
     * 构造函数
     */
    public TageList() {
        
    }
    
    // 
    /**
     * 给iterator中的每个标签赋值
     * 
     * @param sheet excel中指定的sheet
     * @param excelCell excel中指定的cell
     * @param dataSource 数据源
     */
    private void setList(HSSFSheet sheet, ExcelCell excelCell, List<Object> dataSource) {
        
        int iCellNo = excelCell.getCellno();
        int iRowNo = excelCell.getRowno();
        int iSize = dataSource.size();
        String strMethod = ExcelUtil.getKeyWord(excelCell.getTagStr(), "method");
        
        HSSFRow objRow;
        HSSFCell objCell;
        HSSFCellStyle objStyle;
        Object objValue;
        if (iSize > 0) {
            for (int i = 0; i < iSize; i++) {
                objRow = sheet.getRow(i + iRowNo);
                if (objRow == null) {
                    objRow = sheet.createRow(i + iRowNo);
                }
                // row.setHeightInPoints(rowHeight);
                objCell = objRow.getCell(iCellNo);
                if (objCell == null) {
                    objCell = objRow.createCell(iCellNo);
                }
                objStyle = excelCell.getStyle();
                objStyle.setWrapText(true);
                objCell.setCellStyle(objStyle);
                // objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
                
                // 判断是否是序号
                if (TageList.ORDER.equals(strMethod.trim())) {
                    objCell.setCellValue(i + 1);
                } else {
                    try {
                        objValue = ExcelUtil.getValue(dataSource.get(i), strMethod);
                        if (objValue == null) {
                            objCell.setCellValue(" ");
                        } else {
                            String strValue = objValue.toString();
                            if (strValue.startsWith(".")) {
                                strValue = "0" + strValue;
                            }else if(strValue.startsWith("-.")){
                                strValue = strValue.replace("-.", "-0.");
                            }else if(strValue.endsWith(".0")){
                                strValue = strValue.substring(0, strValue.lastIndexOf(".0"));
                            }
                            objCell.setCellValue(strValue);
                        }
                    } catch (ExportExcelException ex) {
                        loger.error(ex.toString(), ex);
                    }
                }
            }
        } else {
            objRow = sheet.createRow(iRowNo);
            // row.setHeightInPoints(rowHeight);
            objCell = objRow.createCell(iCellNo);
            objStyle = excelCell.getStyle();
            objStyle.setWrapText(true);
            // objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            objCell.setCellValue("");
        }
    }
    
    /**
     * 实现 ITageDeal 接口中的方法,处理excel指定sheet中的"iterator"标签.
     * 
     * @param sheet excel中指定的sheet
     * @param dataSource 数据源
     * @param alTages 所有的标签信息.
     */
    @SuppressWarnings("unchecked")
    public void setValue(HSSFSheet sheet, Map<String, Object> dataSource, List<ExcelCell> alTages) {
        int iTagesSize = alTages.size();
        int iListMaxRow = 0;
        int iListMinRow = 0;
        int iFirstRow = 0;
        int iLastRow = 0;
        int iLastRowNum = sheet.getLastRowNum();
        List<Object> lstDataObject;
        ExcelCell objExcelCell;
        
        for (int i = 0; i < iTagesSize; i++) {
            objExcelCell = alTages.get(i);
            lstDataObject = (ArrayList<Object>) dataSource.get("list");
            if (lstDataObject == null || lstDataObject.size() == 0) {
                sheet.removeRow(sheet.getRow(iLastRowNum));
                break;
            }
            iFirstRow = objExcelCell.getRowno();
            iLastRow = lstDataObject.size() + iFirstRow;
            if (iListMaxRow < iLastRow) {
                iListMaxRow = iLastRow;
            }
            if (iListMinRow < iFirstRow) {
                iListMinRow = iFirstRow;
            }
        }
        
        for (int i = iListMinRow; i < iLastRowNum; i++) {
            if (sheet.getRow(i) == null) {
                iListMinRow++;
            } else {
                break;
            }
        }
        iListMinRow++;
        
        if (iListMaxRow - iListMinRow > 0) {
            sheet.shiftRows(iLastRowNum, iListMinRow, iListMaxRow - iListMinRow);
        }
        
        for (int i = 0; i < iTagesSize; i++) {
            objExcelCell = alTages.get(i);
            lstDataObject = (ArrayList<Object>) dataSource.get("list");
            if (lstDataObject == null || lstDataObject.size() == 0) {
                break;
            }
            setList(sheet, objExcelCell, lstDataObject);
        }
        
    }
    
    public static void main(String[] args) {
        String ss="222.0";
        System.out.println(ss.substring(0, ss.lastIndexOf(".0")));
    }
}
